package com.fbs.wowo.manage.service.imp;

import com.fbs.wowo.manage.entity.Permission;
import com.fbs.wowo.manage.entity.Role;
import com.fbs.wowo.manage.mapper.PermissionMapper;
import com.fbs.wowo.manage.mapper.RoleMapper;
import com.fbs.wowo.manage.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;


@Service("authService")
@Transactional
public class AuthServiceImp implements AuthService {

    /*******************************
     *
     * 权限管理
     *
     *******************************/
    /**
     * 按用户id查找角色
     * @param userId 用户 主键id
     * @return Role list
     */
    @Override
    public List<Role> findByUserId(String userId) {
        return roleMapper.findByUserId(userId);
    }

    /**
     * 按角色查找权限
     * @param id 角色 主键id
     * @return Permission list
     */
    @Override
    public List<Permission> findByRoleId(Integer id) {
        return permissionMapper.findByRoleId(id);
    }

    /**
     * 按用户id查找角色字符串集合
     * @param userId
     * @return role string set
     */
    @Override
    public Set<String> findRoleStr(String userId) {
        return roleMapper.findRoleStr(userId);
    }

    /**
     * 按角色查找权限
     * @param userId 角色 主键id
     * @return Permission list
     */
    @Override
    public Set<String> findPermissionStr(String userId) {
        return permissionMapper.findPermissionStr(userId);
    }

    /**
     * 创建权限
     */
    @Override
    public void createPermission(Permission permission) {
        permissionMapper.insertSelective(permission);
    }

    /**
     * 角色绑定权限
     * 增添中间表
     */
    @Override
    public void addPermissionToRole(Integer roleId, Integer permissionId) {
        permissionMapper.addPermissionToRole(roleId, permissionId);
    }

    /**
     * 创建角色
     */
    @Override
    public void createRole(Role role) {
        roleMapper.insertSelective(role);
    }

    /**
     * 用户绑定角色
     */
    @Override
    public void addRoleToUser(String userId, Integer roleId) {
        roleMapper.addRoleToUser(userId, roleId);
    }

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
}
